package com.ljc.service.impl.inner;

import com.ljc.constant.RedisConstant;
import com.ljc.model.dto.UpdateUserInterfaceInfoDTO;
import com.ljc.service.InnerUserInterfaceInfoService;
import com.ljc.service.UserInterfaceInfoService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

@DubboService
public class InnerUserInterfaceInfoServiceImpl implements InnerUserInterfaceInfoService {

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public boolean invokeCount(long interfaceInfoId, long userId) {
        boolean result = userInterfaceInfoService.invokeCount(interfaceInfoId, userId);
        // 数据库写成功，在Redis中记录接口的调用情况，用来进行接口排行
        if (result) {
            stringRedisTemplate.opsForZSet().incrementScore(RedisConstant.ANALYSIS_INVOKE_INTERFACE, String.valueOf(interfaceInfoId), 1);
        }
        return result;
    }

    @Override
    public boolean checkUserInvokeAuth(Long userId, Long interfaceId) {
        return userInterfaceInfoService.checkUserInvokeAuth(userId, interfaceId);
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public boolean updateUserInterfaceInfo(UpdateUserInterfaceInfoDTO updateUserInterfaceInfoDTO) {
        return userInterfaceInfoService.updateUserInterfaceInfo(updateUserInterfaceInfoDTO);
    }
}
